#include "SampleBufferInt16.h"
#include "utility.h"

void SampleBufferInt16AppendDataBack(SampleBufferInt16 *sampleBuffer, int16_t data)
{
    if(!sampleBuffer->isInit)
        return;
    if (sampleBuffer->bufferIndex < sampleBuffer->capacity) {
        sampleBuffer->buffer[sampleBuffer->bufferIndex] = data;
        sampleBuffer->bufferIndex++;
        if (sampleBuffer->bufferIndex == sampleBuffer->capacity) {
            sampleBuffer->isFull = true;
        }
    } else {
        for (int i = 0; i < sampleBuffer->capacity - 1; i++) {
            sampleBuffer->buffer[i] = sampleBuffer->buffer[i + 1];
        }
        sampleBuffer->buffer[sampleBuffer->capacity - 1] = data;
    }
}

void SampleBufferInt16Init(SampleBufferInt16 *sampleBuffer, int16_t *buffer, int16_t capacity)
{
    sampleBuffer->isFull = false;
    sampleBuffer->buffer = buffer;
    sampleBuffer->capacity = capacity;
    sampleBuffer->bufferIndex = 0;
    sampleBuffer->appendDataBack = SampleBufferInt16AppendDataBack;
    sampleBuffer->getRootMeanSquare = RootMeanSquareCal16;
    sampleBuffer->isInit = true;
}



